package com.deyuanyun.pic.ctrl.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.common.util.web.WebUtil;
import com.deyuanyun.pic.ctrl.controller.dto.AcdcDisturbDTO;
import com.deyuanyun.pic.ctrl.controller.dto.AcdcDisturbVO;
import com.deyuanyun.pic.ctrl.service.AcdcDisturbService;
import com.deyuanyun.pic.pipe.controller.BaseController;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.joda.time.LocalDate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Jerry
 * @Description 管理仪表板---交直流干扰控制层
 * @date 2016-09-07
 */
@Controller
@RequestMapping("/corrctrl/acdcDisturb")
public class AcdcDisturbController extends BaseController {

    private static String rootPath = "/template/excl/ctrl/";

    @Resource
    private AcdcDisturbService acdcDisturbService;

    //初始化进入进入页面
    @RequestMapping("/init.html")
    public ModelAndView init(ModelMap model) {

        return forword("/corrctrl/dashboard/cathodicProtect", model);
    }

    //根据条件查询
    @RequestMapping("/queryAcdc.json")
    @ResponseBody
    public void queryAcdc(ModelMap model, AcdcDisturbVO acdcDisturbVO) {
        PageJsonBean jsonBean = new PageJsonBean();
        List<AcdcDisturbDTO> disturb = acdcDisturbService.queryData(acdcDisturbVO);
        jsonBean.setList(disturb);
        AjaxSupport.sendSuccessText("success", jsonBean);
    }

    /**
     * 导出交直流干扰excel
     *
     * @param response
     */
    @RequestMapping("/exportAcdcExcel.json")
    @ResponseBody
    public void exportAcdcExcel(HttpServletResponse response, AcdcDisturbVO AcdcDisturbVO) throws IOException {
        String filePath = rootPath + "instrument-acdcdisturb.xlsx";
        List<AcdcDisturbDTO> disturbList = acdcDisturbService.queryData(AcdcDisturbVO);
        List<String[]> listSheet = JXL.toListFormVO(disturbList, "yyyy-MM-dd");
        Integer beginRow = 3;
        Workbook workbook = ExcelUtil.initWorkbook(filePath, listSheet, beginRow);
        String excelName = "交直流干扰管理仪表板" + LocalDate.now().toString("yyyyMMdd") + ".xlsx";
        DownExcelUtil.fillWorkbookHead(workbook);
        WebUtil.setDownHeader(excelName);
        workbook.write(response.getOutputStream());
    }
}
